package com.zz.rabbitmq.work;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DefaultConsumer;
import com.zz.rabbitmq.constant.Constants;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Producer {
    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        // 1.建立连接
        ConnectionFactory factory = new ConnectionFactory();
        // 2.设置参数
        factory.setHost(Constants.IP);
        factory.setPort(Constants.PORT);
        factory.setVirtualHost(Constants.VIRTUAL_HOST);
        factory.setUsername(Constants.USERNAME);
        factory.setPassword(Constants.PASSWORD);
        Connection connection = factory.newConnection();
        // 3.创建 channel
        Channel channel = connection.createChannel();
        // 4.声明一个队列
        channel.queueDeclare(Constants.WORK_QUEUE, true, false, false, null);
        // 5.发送消息
        for (int i = 0; i < 10; i++) {
            String message = "Hello World RabbitMQ!" + i;
            channel.basicPublish("", Constants.WORK_QUEUE, null, message.getBytes());
        }

        // 6.关闭 channel
        channel.close();
        // 7.关闭连接
        connection.close();
    }
}
